/** */
package co.com.umobile.common.eventhandler;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import co.com.umobile.application.GlobalState;
import co.com.umobile.common.Numbers;
import co.com.umobile.common.widget.ProgressHandler;


/**
 * @author sgranada
 * 
 * 27/02/2012
 */
public class GlobalReceiver extends BroadcastReceiver {

  /** globalState is used to transfer data to other activity. */
  private GlobalState globalState;

  /*
   * (non-Javadoc)
   * 
   * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
   */
  @Override
  public void onReceive(Context context, Intent intent) {

		globalState = (GlobalState) context.getApplicationContext();

		initUpdate(context);

  }


  /**
   * start the update service.
   * @param context context.
   */
  private void initUpdate(Context context) {

    if (globalState.getUpdateQueue() != null && !globalState.getUpdateQueue().isEmpty()
        && globalState.getHandlerByService() != null) {

      Integer serviceCode = globalState.getUpdateQueue().poll();

      if (globalState.getHandlerByService().containsKey(serviceCode)) {

        ProgressHandler handler = globalState.getHandlerByService().get(serviceCode);

        if (handler != null) {
          handler.obtainMessage(Numbers.SIX).sendToTarget();
        }
      }
    }

  }
}
